/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ntb.controllers;

import com.ntb.common.PaginationUtil;
import com.ntb.common.SecuredAction;
import com.ntb.controllers.forms.LocationListingForm;
import com.ntb.entities.Locations;
import com.ntb.models.LocationsDAO;
import com.ntb.models.impl.LocationsDAOImpl;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Thinh Hung
 */
public class CostHistoryListingAction extends SecuredAction {

    public ActionForward ntbExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        LocationsDAO locationsDao = new LocationsDAOImpl();
        LocationListingForm locationListingForm = (LocationListingForm) form;

        String baseUrl = request.getRequestURI() + "?";

        String locationName = null;
        int page = 1;

        locationName = locationListingForm.getName();
        locationName = "".equals(locationName) ? null : locationName;
        if (locationName != null) {
            baseUrl += "name=" + locationName;
        }

        try {
            int i = Integer.parseInt(request.getParameter("page"));
            page = i > 1 ? i : 1;
        } catch (Exception ex) {
        }

        Iterator<Locations> locationList = locationsDao.getList(locationName, page);

        request.setAttribute("locationList", locationList);

        PaginationUtil paginationUtil = new PaginationUtil();
        paginationUtil.setBaseUrl(baseUrl);
        paginationUtil.setNumPages(locationsDao.getPageCount(locationName));
        paginationUtil.setCurPage(page);
        request.setAttribute("pagination", paginationUtil.createLinks());

        return mapping.getInputForward();
    }
}
